<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!--
 * FCKeditor - The text editor for Internet - http://www.fckeditor.net
 * Copyright (C) 2003-2009 Frederico Caldeira Knabben
 *
 * == BEGIN LICENSE ==
 *
 * Licensed under the terms of any of the following licenses at your
 * choice:
 *
 *  - GNU General Public License Version 2 or later (the "GPL")
 *    http://www.gnu.org/licenses/gpl.html
 *
 *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
 *    http://www.gnu.org/licenses/lgpl.html
 *
 *  - Mozilla Public License Version 1.1 or later (the "MPL")
 *    http://www.mozilla.org/MPL/MPL-1.1.html
 *
 * == END LICENSE ==
 *
 * This page shows all resources available in a folder in the File Browser.
-->
<html>
<head>
	<title>Resources</title>
	<link href="browser.css" type="text/css" rel="stylesheet">
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<script type="text/javascript" src="js/common.js"></script>
	<script type="text/javascript">

var oListManager = new Object() ;

oListManager.Clear = function()
{
	document.body.innerHTML = '' ;
}

function ProtectPath(path)
{
	path = path.replace( /\\/g, '\\\\') ;
	path = path.replace( /'/g, '\\\'') ;
	return path ;
}

oListManager.GetFolderRowHtml = function( folderName, folderPath )
{
	// Build the link to view the folder.
	var sLink = '<a href="#" onclick="OpenFolder(\'' + ProtectPath( folderPath ) + '\');return false;">' ;

	return '<tr>' +
			'<td width="16">' +
				sLink +
				'<img alt="" src="images/Folder.gif" width="16" height="16" border="0"><\/a>' +
			'<\/td><td nowrap colspan="2">&nbsp;' +
				sLink +
				folderName +
				'<\/a>' +
		'<\/td><\/tr>' ;
}

oListManager.GetFileRowHtml = function(fileName, fileUrl, fileSize, filetime) {
    // Build the link to view the folder.
    var sLink = '<a href="#" onclick="OpenFile(\'' + ProtectPath(fileUrl) + '\');return false;">';

    // Get the file icon.
    var sIcon = oIcons.GetIcon(fileName);
    var tfileurl = 'images/icons/' + sIcon + '.gif';
    var tw = "16";
    if (fileUrl.indexOf("image") > -1) {
        tfileurl = fileUrl;
        tw = "50";
    }
    return '<tr>' +
			'<td width="16">' +
				sLink +
				'<img alt="" src="' + tfileurl + '" width="' + tw + '" height="' + tw + '" border="0"><\/a>' + //
			'<\/td><td>&nbsp;' +
				sLink +
				fileName +
				'<\/a>' +
			'<\/td><td>&nbsp;' + filetime + '</td><td align="right" nowrap>&nbsp;' +
				fileSize +
				' KB' +
		'<\/td><\/tr>';
}

function OpenFolder( folderPath )
{
	// Load the resources list for this folder.
	window.parent.frames['frmFolders'].LoadFolders( folderPath ) ;
}

function OpenFile( fileUrl )
{
	window.top.opener.SetUrl( encodeURI( fileUrl ).replace( '#', '%23' ) ) ;
	window.top.close() ;
	window.top.opener.focus() ;
}
var _resourceType;
var _folderPath;
function LoadResources( resourceType, folderPath )
{
	oListManager.Clear() ;
	oConnector.ResourceType = resourceType ;
	oConnector.CurrentFolder = folderPath ;
	oConnector.SendCommand( 'GetFoldersAndFiles', null, GetFoldersAndFilesCallBack ) ;
}
function LoadResourcesName() {
    oListManager.Clear();
    oConnector.ResourceType = _resourceType;
    oConnector.CurrentFolder = _folderPath;
    oConnector.SendCommand('GetFoldersAndFiles', null, GetFoldersAndFilesCallBack);
}
function LoadResourcesTime() {
    oListManager.Clear();
    oConnector.ResourceType = _resourceType;
    oConnector.CurrentFolder = _folderPath;
    oConnector.SendCommand('GetFoldersAndFilesTime', null, GetFoldersAndFilesCallBack);
}


function Refresh()
{
	LoadResources( oConnector.ResourceType, oConnector.CurrentFolder ) ;
}
var oNodes;
var oArray = new Array();
var _fckXml;
var sCurrentFolderPath;
var sCurrentFolderUrl;
function GetFoldersAndFilesCallBack( fckXml ) {
    ist = false;
	if ( oConnector.CheckError( fckXml ) != 0 )
		return ;

	// Get the current folder path.
	var oFolderNode = fckXml.SelectSingleNode( 'Connector/CurrentFolder' ) ;
	if ( oFolderNode == null )
	{
		alert( 'The server didn\'t reply with a proper XML data. Please check your configuration.' ) ;
		return ;
	}
	sCurrentFolderPath	= oFolderNode.attributes.getNamedItem('path').value ;
	sCurrentFolderUrl	= oFolderNode.attributes.getNamedItem('url').value ;

//	var dTimer = new Date() ;

	var oHtml = new StringBuilder('<table id="tableFiles" cellspacing="1" cellpadding="0" width="100%" border="0">');
	oHtml.Append('<tr bgcolor="#B3CFEC">' +
			'<td width="16" align="center">&nbsp;Image<\/td><td align="center"><b>Name</b>' +
			'<\/td><td align="center"><a href="javascript:sort(\'time\');">Time<\/a></td><td align="center">Size<\/td><\/tr>');
	// Add the Folders.
	_fckXml = fckXml;
	oNodes = fckXml.SelectNodes( 'Connector/Folders/Folder' ) ;
	for ( var i = 0 ; i < oNodes.length ; i++ )
	{
		var sFolderName = oNodes[i].attributes.getNamedItem('name').value ;
		oHtml.Append( oListManager.GetFolderRowHtml( sFolderName, sCurrentFolderPath + sFolderName + "/" ) ) ;
	}
	
	// Add the Files.
	oNodes = fckXml.SelectNodes( 'Connector/Files/File' ) ;
	for ( var j = 0 ; j < oNodes.length ; j++ )
	{
		var oNode = oNodes[j] ;
		var sFileName = oNode.attributes.getNamedItem('name').value ;
		var sFileSize = oNode.attributes.getNamedItem('size').value ;
		var sFileTime = oNode.attributes.getNamedItem('time').value;
		
		// Get the optional "url" attribute. If not available, build the url.
		var oFileUrlAtt = oNodes[j].attributes.getNamedItem('url') ;
		var sFileUrl = oFileUrlAtt != null ? oFileUrlAtt.value : sCurrentFolderUrl + sFileName ;

		oArray[j] = [sFileName, sFileSize, sFileTime, sFileUrl];

		oHtml.Append(oListManager.GetFileRowHtml(sFileName, sFileUrl, sFileSize, sFileTime));
	}

	oHtml.Append('<\/table>');

	sort("time");

//	document.body.innerHTML = oHtml.ToString() ;

//	window.top.document.title = 'Finished processing in ' + ( ( ( new Date() ) - dTimer ) / 1000 ) + ' seconds' ;

}
var ist = false;
function sort(type) {
    var oHtml = new StringBuilder('<table id="tableFiles" cellspacing="1" cellpadding="0" width="100%" border="0">');
    if (type && type == "time") {
        if (ist == false) {
            /*for (var j = 0; j < oArray.length-1; j++) {//冒泡排序
                var ise = false;
                for (var k = oArray.length-1; k > j; k--) {
                    if (oArray[k][2] > oArray[k-1][2]) {
                        var t = oArray[k];
                        oArray[k] = oArray[k - 1];
                        oArray[k - 1] = t;
                        ise = true;
                    }

                }
                if (ise == false) {
                    break;
                }
            }*/
            quickSort(oArray, 0, oArray.length - 1);
            ist = true;
        }
        
        oHtml.Append('<tr bgcolor="#B3CFEC">' +
			'<td width="16" align="center">&nbsp;Image<\/td><td align="center"><a href="javascript:sort(\'name\')">Name<\/a>' +
			'<\/td><td align="center"><b>Time</b></td><td align="center">Size<\/td><\/tr>');

        var oNodesF = _fckXml.SelectNodes('Connector/Folders/Folder');
        for (var i = 0; i < oNodesF.length; i++) {
            var sFolderName = oNodesF[i].attributes.getNamedItem('name').value;
            oHtml.Append(oListManager.GetFolderRowHtml(sFolderName, sCurrentFolderPath + sFolderName + "/"));
        }

        for (var j = 0; j < oArray.length; j++) {
            oHtml.Append(oListManager.GetFileRowHtml(oArray[j][0], oArray[j][3], oArray[j][1], oArray[j][2]));
        }
    } else {
        oHtml.Append('<tr bgcolor="#B3CFEC">' +
			    '<td width="16" align="center">&nbsp;Image<\/td><td align="center"><b>Nmae</b>' +
			    '<\/td><td align="center"><a href="javascript:sort(\'time\')">Time</a></td><td align="center">Size<\/td><\/tr>');
        var oNodesF = _fckXml.SelectNodes('Connector/Folders/Folder');
        for (var i = 0; i < oNodesF.length; i++) {
            var sFolderName = oNodesF[i].attributes.getNamedItem('name').value;
            oHtml.Append(oListManager.GetFolderRowHtml(sFolderName, sCurrentFolderPath + sFolderName + "/"));
        }

        for (var j = 0; j < oNodes.length; j++) {
            var oNode = oNodes[j];
            var sFileName = oNode.attributes.getNamedItem('name').value;
            var sFileSize = oNode.attributes.getNamedItem('size').value;
            var sFileTime = oNode.attributes.getNamedItem('time').value;

            // Get the optional "url" attribute. If not available, build the url.
            var oFileUrlAtt = oNodes[j].attributes.getNamedItem('url');
            var sFileUrl = oFileUrlAtt != null ? oFileUrlAtt.value : sCurrentFolderUrl + sFileName;

            oHtml.Append(oListManager.GetFileRowHtml(sFileName, sFileUrl, sFileSize, sFileTime));
        }
    }
    document.body.innerHTML = oHtml.ToString();
}

function quickSort(strDate, left ,right) {//快速排序
  var middle;
  var tempDate;
  var i;  
  var j;
  i = left;
  j = right;
  middle = strDate[parseInt((i + j) / 2)][2];
  do {
      while (strDate[i][2] > middle && i < right)
    i++; // 找出左边比中间值大的数
    while (strDate[j][2] < middle && j > left)
    j--; // 找出右边比中间值小的数
   if (i <= j) { // 将左边大的数和右边小的数进行替换
    tempDate = strDate[i];
    strDate[i] = strDate[j];
    strDate[j] = tempDate;
    i++;
    j--;
   }
  } while (i <= j); // 当两者交错时停止

  if (i < right) {
   quickSort(strDate, i, right);
  }
  if (j > left) {
   quickSort(strDate, left, j);
  }


}

window.onload = function()
{
	window.top.IsLoadedResourcesList = true ;
}
	</script>
</head>
<body class="FileArea">
</body>
</html>
